/*
 * CategoryFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.SQLException;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import com.insigma.yanwenlong.bookkeeping.dao.CommonDao;
import com.insigma.yanwenlong.bookkeeping.pojo.vo.CategoryVo;
import com.insigma.yanwenlong.bookkeeping.service.CategoryService;
import com.insigma.yanwenlong.bookkeeping.service.CategoryServiceImpl;

/**
 *
 * @author  __USER__
 */
public class CategoryFrame extends javax.swing.JFrame {
	private CategoryService categoryService = new CategoryServiceImpl();
	private DefaultTableModel dtm;

	/** Creates new form CategoryFrame */
	public CategoryFrame() {
		this.setTitle("类别维护");
		initComponents();
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		//初始化界面类别
		initCategory();
	}

	private void initCategory() {
		try {
			List<CategoryVo> categoryVos = categoryService.findCategoryVos();
			dtm = (DefaultTableModel) tblCategory.getModel();
			for (CategoryVo categoryVo : categoryVos) {
				dtm.addRow(new Object[] { categoryVo.getId(),
						categoryVo.getCategoryName(),
						categoryVo.getDirectionName() });
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		tblCategory = new javax.swing.JTable();
		btnAddCategory = new javax.swing.JButton();
		btnDeleteCategory = new javax.swing.JButton();
		btnUpdate = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		tblCategory.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "类别编号", "类别名称", "类别方向" }) {
			boolean[] canEdit = new boolean[] { false, false, false };

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		jScrollPane1.setViewportView(tblCategory);

		btnAddCategory.setText("\u6dfb\u52a0\u7c7b\u522b");
		btnAddCategory.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnAddCategoryActionPerformed(evt);
			}
		});

		btnDeleteCategory.setText("\u5220\u9664\u7c7b\u522b");
		btnDeleteCategory
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						btnDeleteCategoryActionPerformed(evt);
					}
				});

		btnUpdate.setText("\u5237\u65b0");
		btnUpdate.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btnUpdateActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addComponent(btnAddCategory)
						.addGap(18, 18, 18).addComponent(btnDeleteCategory)
						.addGap(18, 18, 18).addComponent(btnUpdate))
				.addComponent(jScrollPane1,
						javax.swing.GroupLayout.PREFERRED_SIZE, 375,
						javax.swing.GroupLayout.PREFERRED_SIZE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				layout.createSequentialGroup().addComponent(jScrollPane1,
						javax.swing.GroupLayout.DEFAULT_SIZE, 270,
						Short.MAX_VALUE).addGap(18, 18, 18).addGroup(
						layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(btnAddCategory).addComponent(
										btnDeleteCategory).addComponent(
										btnUpdate)).addGap(25, 25, 25)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents
	
	//点击删除类别
	private void btnDeleteCategoryActionPerformed(java.awt.event.ActionEvent evt) {
		if(tblCategory.getSelectedRow() < 0) {
			JOptionPane.showMessageDialog(this, "你没有选择任何行");
		}else {
			int[] rowCounts = tblCategory.getSelectedRows();
			int categoryId;
			for (int i : rowCounts) {
				categoryId = (Integer)tblCategory.getValueAt(i, 0);
				CommonDao cd = new CommonDao();
				cd.deleteCategoryById(categoryId);
			}
		}
	}

	//点击添加类别
	private void btnAddCategoryActionPerformed(java.awt.event.ActionEvent evt) {
		new AddCategoryFrame().setVisible(true);
	}

	//点击刷新
	private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
		dtm.setRowCount(0);
		initCategory();
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new CategoryFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnAddCategory;
	private javax.swing.JButton btnDeleteCategory;
	private javax.swing.JButton btnUpdate;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable tblCategory;
	// End of variables declaration//GEN-END:variables

}